package com.tenebrous.jestic.controller.server;

import com.rain.ops.sdk.dto.ApiResult;
import com.tenebrous.jestic.constants.ResultCode;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.dto.EncryptionDto;
import com.tenebrous.jestic.entity.server.JesticServer;
import com.tenebrous.jestic.service.server.AuthServerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Project: jestic-server
 * @Package: com.tenebrous.jestic.controller.server
 * @Author: zhangya
 * @CreateTime: 2023-06-02  16:25
 * @Version: 1.0.0
 */
@Validated
@RestController
@RequestMapping("/jestic/server/v1")
public class JesticServerController  extends SupperController {

    @Autowired
    private AuthServerService authServerService;

    @PostMapping("/list")
    public ApiResult serverListByPage(String serverName, String env, Integer serverStatus) {
        return new ApiResult(200, "查询成功。", authServerService.serverListByPage(super.getPageIndex(), super.getPageSize(), serverName, env, serverStatus));
    }

    @PostMapping("/instanceList")
    public ApiResult instanceList(String instanceId, String serverName, Integer env) {
        return new ApiResult(200, "查询成功。", authServerService.instanceList(instanceId,  serverName, env));
    }

    @PostMapping("/createServer")
    public ApiResult createServer(@RequestBody JesticServer jesticServer) {
        authServerService.createServer(super.getCurrentUser() ,jesticServer);
        return new ApiResult<>(ResultCode.C200.getCode(), "新增服务成功");
    }

    @PostMapping("/encryption")
    public ApiResult encryption(@RequestBody EncryptionDto dto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String instanceId = request.getHeader("instanceId");
        String pwd = authServerService.encryption(super.getCurrentUser() ,dto);
        return new ApiResult<>(ResultCode.C200.getCode(), "生成成功", "decrypt{"+pwd+"}");
    }
}
